package com.liang.design;

import com.liang.design.util.Topic;
import com.liang.design.util.TopicRandomUtil;
import lombok.Data;
import org.itstack.demo.design.AnswerQuestion;
import org.itstack.demo.design.ChoiceQuestion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/**
 * @author lsm
 * @ClassName QuestionBank.java
 * @Description TODO
 * @createTime 2023年11月30日 17:01:00
 */
@Data
public class QuestionBank implements Cloneable{

    //考生
    private String candidate;
    //考号
    private String number;

    private ArrayList<ChoiceQuestion> choiceQuestionList = new
            ArrayList<ChoiceQuestion>();
    private ArrayList<AnswerQuestion> answerQuestionList = new
            ArrayList<AnswerQuestion>();

    public QuestionBank append(ChoiceQuestion choiceQuestion){
        choiceQuestionList.add(choiceQuestion);
        return this;
    }

    public QuestionBank append(AnswerQuestion answerQuestion){
        answerQuestionList.add(answerQuestion);
        return this;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        QuestionBank questionBank = (QuestionBank) super.clone();
        questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();
        questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();

        Collections.shuffle(questionBank.answerQuestionList);
        Collections.shuffle(questionBank.choiceQuestionList);

        //选择题乱序
        for (ChoiceQuestion choiceQuestion : questionBank.choiceQuestionList) {
            Topic random = TopicRandomUtil.random(choiceQuestion.getOption(), choiceQuestion.getKey());
            choiceQuestion.setOption(random.getOption());
            choiceQuestion.setKey(random.getKey());
        }

        return questionBank;
    }

    @Override
    public String toString(){
        StringBuilder detail = new StringBuilder("考⽣生： " + candidate +
                "\r\n" +
                "考号： " + number + "\r\n" +
                "--------------------------------------------\r\n" +
                "⼀一、选择题" + "\r\n\n");
        for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
            detail.append("第").append(idx +
                    1).append("题： ").append(choiceQuestionList.get(idx).getName()).append("\r\n");
            Map<String, String> option =
                    choiceQuestionList.get(idx).getOption();
            for (String key : option.keySet()) {
                detail.append(key).append("： ").append(option.get(key)).append("\r\n");;
            }
            detail.append("答 案： ").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
        }
        detail.append("⼆二、问答题" + "\r\n\n");
        for (int idx = 0; idx < answerQuestionList.size(); idx++) {
            detail.append("第").append(idx +
                    1).append("题： ").append(answerQuestionList.get(idx).getName()).append("\r\n");
            detail.append("答案： ").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");
        }
        return detail.toString();
    }
}
